package com.panda.mall.elastic.utils;

import com.panda.mall.common.mallWares.entity.MallGoodsSpuInfo;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * author: tcy
 * createDate: 2023/3/2
 * description: 数据解析工具类
 */
@Component
public class ParseDataUtil {

    /**
     * 从京东爬取数据
     * @param keyword
     * @return
     * @throws Exception
     */
    public List<MallGoodsSpuInfo> parseData(String keyword) throws Exception {

        URL url = new URL("https://search.jd.com/Search?keyword=" + keyword);

        Document document = Jsoup.parse(url, 30000);

        Element elementById = document.getElementById("J_goodsList");

        Elements elementsByTag = elementById.getElementsByTag("li");

        List<MallGoodsSpuInfo> goodsSpuInfos = new ArrayList<>();

        for (int i = 0; i < elementsByTag.size(); i++) {
            MallGoodsSpuInfo mallGoodsSpuInfo = new MallGoodsSpuInfo();
            mallGoodsSpuInfo.setId((long) (i + 1));
            mallGoodsSpuInfo.setSupName(elementsByTag.get(i).getElementsByClass("p-name").eq(0).text());
            mallGoodsSpuInfo.setSupDescription(elementsByTag.get(i).getElementsByTag("img").attr("data-lazy-img"));

            goodsSpuInfos.add(mallGoodsSpuInfo);
        }
        return ObjectUtils.isEmpty(goodsSpuInfos) ? null : goodsSpuInfos;
    }
}

